package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DWaybillFeeTypeService;
import cn.huiyunche.base.service.mappers.DWaybillFeeTypeMapper;
import cn.huiyunche.base.service.model.DWaybillFeeType;
import cn.huiyunche.base.service.model.DWaybillFeeTypeExample;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.service.base.tms.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/11/2 下午5:26
 */
@Service
public class DWaybillFeeTypeServiceImpl implements DWaybillFeeTypeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DWaybillFeeTypeServiceImpl.class);

    @Autowired
    private DWaybillFeeTypeMapper dWaybillFeeTypeMapper;

    @Override
    public Integer getIdByConditions(String feeName) {
        LOGGER.info("getIdByConditions param : {}", feeName);
        if (StringUtils.isBlank(feeName)) {
            LOGGER.error("getIdByConditions param must not be null");
            throw new IllegalArgumentException("费用名称不能为空");
        }

        DWaybillFeeTypeExample example = new DWaybillFeeTypeExample();
        example.createCriteria().andFeeNameEqualTo(feeName);
        List<DWaybillFeeType> list = dWaybillFeeTypeMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0).getId();
        }

        DWaybillFeeType feeType = new DWaybillFeeType();
        feeType.setFeeName(feeName);
        dWaybillFeeTypeMapper.insertSelective(feeType);

        return feeType.getId();
    }
}
